

export class visionTools
{
    constructor(page, scrapingTools)
    {
        this.page = page;
        this.scrapingTools = scrapingTools ;
    }

    async screenshot({})
    {
        await this.scrapingTools.scrape({},false,true) ; 
        try
        {
            const screenshotBuffer = await this.page.ref.screenshot(
            {
                type: 'jpeg',
                quality: 100
            });
            const base64Image = screenshotBuffer.toString('base64');
            return "data:image/jpeg;base64," + base64Image;
        
        }
        catch(error)
        {
            console.error(error) ;
            return `failed to take screenshot`;
        }
    }

    async passCaptcha({})
    {
        await this.page.ref.evaluate(()=>
        {
            function waitForUser(message = "Solve the captcha, then click to continue") 
            {
                return new Promise(resolve => 
                {
                    const btn = document.createElement("button");
                    btn.innerText = message;

                    // basic styling so it stands out
                    btn.style.position = "fixed";
                    btn.style.top = "20px";
                    btn.style.right = "20px";
                    btn.style.zIndex = "999999";
                    btn.style.padding = "10px 15px";
                    btn.style.background = "#4caf50";
                    btn.style.color = "white";
                    btn.style.border = "none";
                    btn.style.borderRadius = "8px";
                    btn.style.cursor = "pointer";
                    btn.style.boxShadow = "0px 2px 6px rgba(0,0,0,0.2)";

                    btn.onclick = () => {
                    btn.remove();
                    resolve();
                    };

                    document.body.appendChild(btn);
                });
            }
            return waitForUser() ;
        })
        return "Passed Captcha" ;
    }
}